<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ include file="validausuario.jsp"%>
<%@ page import="com.are.odisea.*"%>
<%@ page import="java.sql.*"%>
<%
	db conexion = new db();
	
	GestionDepartamento gd = new GestionDepartamento(conexion);
	ArrayList<Departamento> departamentos = new ArrayList<Departamento>();
	
	departamentos = gd.List();
	Long total_asignadas = 0L;
	ResultSet rs = null;
	boolean rsIsEmpty = true;
	if (request.getParameter("departamento") != null) {
		String dpto = (String)request.getParameter("departamento");
		String sql = "";
		String usuario = (String)session.getAttribute("usuario");
		GestionUsuario gu = new GestionUsuario(conexion);
		GestionMantenimiento gm = new GestionMantenimiento(conexion,null);
		if (gu.CountPersonal(usuario) == 0) {
				sql = "select * from tablero_ejecucion where fecha = CURRENT_DATE() and CODIGO_DEPARTAMENTO = ? order by descripcion_departamento,nombre,apellido";
				java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
				total_asignadas = gm.TotalAsignadas(dpto);
				pst.setString(1, dpto);
				rs = conexion.Query(pst);
		}else {
				sql = "select * from tablero_ejecucion_usuario where fecha = CURRENT_DATE() and CODIGO_DEPARTAMENTO = ? and USUARIO = ? order by descripcion_departamento,nombre,apellido";
				java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
				total_asignadas = gm.TotalAsignadas(dpto,usuario);
				pst.setString(1, dpto);
				pst.setString(2, usuario);
				rs = conexion.Query(pst);
		}
		rsIsEmpty = !rs.next();
	
	}
	
	int fila = 0;
	int total= 0;
	int tecnicos = 0;
	int[] total_hora = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	GestionBandeja gb = new GestionBandeja(conexion);
	
	
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Tablero de Ejecucion</title>
<link rel="stylesheet" href="themes/redmond/jquery.ui.all.css">
<LINK REL="stylesheet" TYPE="text/css" HREF="main.css">
<script src="js/jquery.js" language="JavaScript"></script>
<script src="ui/jquery.ui.core.js" language="JavaScript"></script>
<script src="ui/jquery.ui.widget.js" language="JavaScript"></script>
<script src="ui/jquery.ui.button.js"></script>
<script src="js/highcharts.js"></script>
<script src="js/modules/exporting.js"></script>

<script type="text/javascript">
var nombres = [];
var valores = [];

$(function() {
	$( "input:submit, a, button", ".demo" ).button();
	$( "input:button, a, button", ".demo" ).button();
});
</script>
<script language="javascript"> 
$(document).ready(function() { 
     $(".botonExcel").click(function(event) { 
     $("#datos_a_enviar").val( $("<div>").append( $("#Exportar_a_Excel").eq(0).clone()).html()); 
     $("#FormularioExportacion").submit(); 
}); 
}); 
</script>
</head>
<body>
<%@include file="header.jsp"%>
<div class="contencenter demo">
	<h2>Tablero de Ejecucion Mantenimiento</h2>
	
	<form name="form1" method="post" action="">
	Seleccione departamento: 
	<select name="departamento">
		<% for ( Departamento departamento: departamentos ) { %>
		<option value="<%= departamento.getCodigo() %>" <%= (departamento.getCodigo().equals((String)request.getParameter("departamento"))?"selected":"") %> ><%= departamento.getDescripcion() %></option>
		<% } %>
	</select>
	<input type="submit" name="enviar" value="Enviar"> <a href="TableroHistorico.jsp">Historico</a>  <a href="Tablero.jsp">Regresar</a>
	</form>
	<% if (!rsIsEmpty)  {%>
	<form action="ExportExcel.jsp" method="post" target="_blank" id="FormularioExportacion">
	<p>Exportar a Excel  <img src="images/Download.png" class="botonExcel" /></p> 
	<input type="hidden" id="datos_a_enviar" name="datos_a_enviar" /> 
	</form> 	
	<table id="Exportar_a_Excel">
		<tr>
			<th>Cedula</th>
			<th>Nombre y Apellido</th>
			<th>Departamento</th>
			<th>Fecha</th>
			<th>0 - 6 AM</th>
			<th>7 AM</th>
			<th>8 AM</th>
			<th>9 AM</th>
			<th>10 AM</th>
			<th>11 AM</th>
			<th>12 MD</th>
			<th>1 PM</th>
			<th>2 PM</th>
			<th>3 PM</th>
			<th>4 PM</th>
			<th>5 PM</th>
			<th>6 PM</th>
			<th>7 PM</th>
			<th>8 PM</th>
			<th>9  - 12 PM</th>
			<th>TOTAL</th>
		</tr>

		<%
			do {
		%>
		<tr <%=fila % 2 == 0 ? "class='odd'" : ""%>>
			<td><%= (String)rs.getString("CEDULA") %><script type="text/javascript"> nombres.push('<%= (String)rs.getString("NOMBRE") %> <%= (String)rs.getString("APELLIDO") %>'); </script> </td>
			<td><%= (String)rs.getString("NOMBRE") %> <%= (String)rs.getString("APELLIDO") %></td>
			<td><%= (String)rs.getString("DESCRIPCION_DEPARTAMENTO") %></td>
			<td><%= (String)rs.getString("FECHA") %></td>
			<td><%= (String)rs.getString("H_0_6") %></td>
			<td><%= (String)rs.getString("H7") %></td>
			<td><%= (String)rs.getString("H8") %></td>
			<td><%= (String)rs.getString("H9") %></td>
			<td><%= (String)rs.getString("H10") %></td>
			<td><%= (String)rs.getString("H11") %></td>
			<td><%= (String)rs.getString("H12") %></td>
			<td><%= (String)rs.getString("H13") %></td>
			<td><%= (String)rs.getString("H14") %></td>
			<td><%= (String)rs.getString("H15") %></td>
			<td><%= (String)rs.getString("H16") %></td>
			<td><%= (String)rs.getString("H17") %></td>
			<td><%= (String)rs.getString("H18") %></td>
			<td><%= (String)rs.getString("H19") %></td>
			<td><%= (String)rs.getString("H20") %></td>
			<td><%= (String)rs.getString("H_21_23") %></td>
			<td><%= (String)rs.getString("TOTAL") %> <script type="text/javascript"> valores.push(<%= (String)rs.getString("TOTAL") %>); </script> </td>
		</tr>
		<%
			total_hora[0] += rs.getInt("H_0_6");
			total_hora[1] += rs.getInt("H7");
			total_hora[2] += rs.getInt("H8");
			total_hora[3] += rs.getInt("H9");
			total_hora[4] += rs.getInt("H10");
			total_hora[5] += rs.getInt("H11");
			total_hora[6] += rs.getInt("H12");
			total_hora[7] += rs.getInt("H13");
			total_hora[8] += rs.getInt("H14");
			total_hora[9] += rs.getInt("H15");
			total_hora[10] += rs.getInt("H16");
			total_hora[11] += rs.getInt("H17");
			total_hora[12] += rs.getInt("H18");
			total_hora[13] += rs.getInt("H19");
			total_hora[14] += rs.getInt("H20");
			total_hora[15] += rs.getInt("H_21_23");
			fila++;
			tecnicos++;
			total += rs.getInt("TOTAL");
			}while(rs.next());
		%>
		<tr <%=fila % 2 == 0 ? "class='odd'" : ""%>>
			<td colspan="4"><strong>Total por Hora</strong></td>
			<% int sum = 0;
				for (int x=0; x < total_hora.length; x++)  {%>
				<td><strong><%= total_hora[x] %></strong></td>
			<% 
				sum += total_hora[x];
			} %>
			<td><strong><%= sum %></strong></td>
		</tr>
		
		<tr <%= ++fila % 2 == 0 ? "class='odd'" : ""%>>
			<td colspan="21"><strong>Total Ordenes ejecutadas: <%= total %></strong></td>
		</tr>
		<tr <%= ++fila % 2 == 0 ? "class='odd'" : ""%>>
			<td colspan="21"><strong>Total Ordenes pendientes: <%= total_asignadas %></strong></td>
		</tr>
		
		<tr <%= ++fila % 2 == 0 ? "class='odd'" : ""%>>
			<td colspan="21"><strong>Total Ordenes en bandeja: <%= gb.TotalOrdenesBandeja(2) %></strong></td>
		</tr>
		
		
		<tr <%= ++fila % 2 == 0 ? "class='odd'" : ""%>>
			<td colspan="21"><strong>Total Tecnicos: <%= tecnicos %></strong></td>
		</tr>
	</table>
	
	<div id="container" style="min-width: 400px; height: 800px; margin: 0 auto"></div>
	
</div>
<script type="text/javascript">
var chart;

$(document).ready(function() {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'bar'
        },
        title: {
            text: 'Total de Averias Ejecutadas'
        },
        exporting : {
        	buttons : {
        		exportButton: {
        			enabled : false
        		}
        	}
        },
        subtitle: {
            text: 'Siscos'
        },
        xAxis: {
            categories: nombres,
            title: {
                text: null
            }
        },
        yAxis: {
            min: 0,
            allowDecimals: false,
            title: {
                text: 'Total averias ejecutadas',
                align: 'high'
            },
            labels: {
                overflow: 'justify'
            }
        },
        tooltip: {
            formatter: function() {
                return ''+
                    this.series.name +': '+ this.y;
            }
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: true
                }
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -100,
            y: 100,
            floating: true,
            borderWidth: 1,
            backgroundColor: '#FFFFFF',
            shadow: true
        },
        credits: {
            enabled: false
        },
        series: [{
            name: 'Ordenes',
            data: valores
        }]
    });
});
</script>
<% } %>
</body>
</html>

<%
	conexion.Close();
%>